<?php

namespace App\Events;

use App\Events\Event;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Support\Facades\Mail;
use App\Models\Code;
use Illuminate\Support\Facades\Log;

class SendEmail extends Event implements ShouldBroadcast
{
    use SerializesModels;

    public $code;
    
    /**
     * Create a new event instance.
     *
     * @return void
     */
    public function __construct(Code $code)
    {
        $this->code = $code;
    }

    /**
     * Get the channels the event should be broadcast on.
     *
     * @return array
     */
    public function broadcastOn()
    {
        $res = Mail::raw($this->code->content,function($message){
            $message->from('master2@cimers.com', '青苜网');
            $message ->to($this->code->phone)->subject('青苜网验证码');
        });
        if($res)
        {
            $this->code->update(['status'=>2]);
        }
        else 
        {
            $this->code->update(['status'=>3]);
            Log::info(['邮件发送失败'=>$res]);
        }
        return ['code.'.$this->code->id];
    }
}
